//全局消息中心

// 注入信息的保存list
var backList = [];

/**
 * 注册需要接收消息中心的返回方法
 */
function register(key, value) {
  var _hasKey = false;
  for (var item in backList) {
    if (backList[item]["name"] == key) {
      _hasKey = true;
      backList[item]["value"] = value;
      return;
    }
  }
  if (!_hasKey) {
    var tempObj = {};
    tempObj.name = key;
    tempObj.value = value;
    backList.push(tempObj);
  }
}

/**
 * 派发全局消息
 */
function sendGlobalMessage(message, value) {
  for (var item in backList) {
    backList[item]["value"](message, value);
  }
}

/**
 * 给单独某一个页面发送消息
 */
function sendSingleMessage(page, message, value) {
  for (var item in backList) {
    if (page == backList[item]["name"]) {
      backList[item]["value"](message, value);
      break;
    }

  }
}

module.exports = {
  reg: register,
  sendG: sendGlobalMessage,
  sendS: sendSingleMessage
}